/*
Write a menu-deriven program to perform the following matrix operations-
 1. Addition   2. Multiplication  3. Transposition

Program by Thomas Jacob & Syam Kumar -------January 1, 2001
*/

#include<stdio.h>
#include<conio.h>

char choice;
int mat1[5][5],mat2[5][5],result[5][5];
int i,j,k,m,n,r,m2,n2;

void resdisp(int x,int y)
{
		printf("\nResult\n");
		for(i=1;i<=x;i++)
		 {
		 for(j=1;j<=y;j++)
		  printf("%d ",result[i][j]);
                 printf("\n");
		 }
		getch();
}

void adder()
{
		printf("MATRIX ADDITION\n");
		printf("\nNo.of rows : "); scanf("%d",&m);
		printf("No.of cols : "); scanf("%d",&n);
		printf("\n\nEnter matrix 1\n");
		for(i=1;i<=m;i++)
		 for(j=1;j<=n;j++)
		  scanf("%d",&mat1[i][j]);
		printf("\nEnter matrix 2\n");
		for(i=1;i<=m;i++)
		 for(j=1;j<=n;j++)
		  scanf("%d",&mat2[i][j]);
                for(i=1;i<=m;i++)
		 for(j=1;j<=n;j++)
		  result[i][j]=mat1[i][j]+mat2[i][j];
		resdisp(m,n);
}

void multiplier()
{
		printf("MATRIX MULTIPLICATION\n");
		printf("\nNo.of rows for matrix 1: "); scanf("%d",&m);
		printf("No.of cols for matrix 1: "); scanf("%d",&n);
		printf("\nNo.of rows for matrix 2: "); scanf("%d",&m2);
		printf("No.of cols for matrix 2: "); scanf("%d",&n2);

		if (n!=m2) printf("\nNot conformable for multiplication.\n");		if(n==m2)
		if(n==m2)
		 {
			r=n2;
			printf("\n\nEnter matrix 1\n");
			for(i=1;i<=m;i++)
			 for(j=1;j<=n;j++)
			  scanf("%d",&mat1[i][j]);
			printf("\nEnter matrix 2\n");
			for(i=1;i<=n;i++)
			 for(j=1;j<=r;j++)
			  scanf("%d",&mat2[i][j]);

			for(i=1;i<=m;i++)
			 for(j=1;j<=r;j++)
			  {
			   result[i][j]=0;
			   for(k=1;k<=n;k++)
			    result[i][j]=result[i][j] +mat1[i][k]*mat2[k][j];
			  }
			resdisp(m,r);
		 };
}

void transposer()
 {
		printf("MATRIX TRANSPOSITION\n");
		printf("\nNo.of rows : "); scanf("%d",&m);
		printf("No.of cols : "); scanf("%d",&n);
		printf("\n\nEnter matrix\n");
		for(i=1;i<=m;i++)
		 for(j=1;j<=n;j++)
		  scanf("%d",&mat1[i][j]);
		for(i=1;i<=n;i++)
		 for(j=1;j<=m;j++)
		  result[i][j]=mat1[j][i];
		resdisp(n,m);
}

void main()
{
 do
 {
 clrscr();
 printf("-------MENU--------\n");
 printf("<A>. . . Add\n");
 printf("<M>. . . Multiply\n");
 printf("<T>. . . Transpose\n\n");
 printf("<X>. . . Exit\n");
 printf("-------------------\n");
 printf("Choice: ");
 choice=toupper(getch());
 clrscr();

 switch(choice)
 {
  case 'A' : adder(); break;
  case 'M' : multiplier(); break;
  case 'T' : transposer(); break;
 }

 }
 while (choice!='X');
}
